package com.zxb.base.watch.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.zxb.base.watch.util.WatchMap;
import com.zxb.base.watch.util.WatchUtil;
import com.zxb.base.watch.vo.WatchVo;
import com.zxb.core.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

/**
 * @author Zxb
 * 监控文件变化
 */
@RestController
@RequiredArgsConstructor
public class WatchController {
    private final WatchUtil watchUtil;

    @GetMapping("/un/watch/list")
    public R listWatch() {
        return R.success(WatchMap.voMap);
    }

    @PostMapping("/un/watch/start")
    public R startWatch(@RequestBody WatchVo watchVo) {
        File f = new File(watchVo.getFilePath());
        if (!f.exists()) {
            return R.error("文件不存在");
        }
        String code = watchVo.getCode();
        if (WatchMap.contains(code)) {
            WatchVo oldWatchVo = WatchMap.getVo(code);
            oldWatchVo.setIsOpen(true);
            watchUtil.startWatch(oldWatchVo);
            return R.success();
        }
        watchVo.setFolderPath(f.getParent());
        watchVo.setFileName(f.getName());
        watchVo.setIsOpen(true);
        watchUtil.startWatch(watchVo);
        return R.success();
    }

    @PostMapping("/un/watch/pause")
    public R pauseWatch(@RequestBody Map<String, String> map) {
        String watchCode = map.get("watchCode");
        if (StrUtil.isBlank(watchCode)) {
            return R.error("请输入监控对象编码");
        }
        if (!WatchMap.contains(watchCode)) {
            return R.error("监控对象不存在");
        }
        WatchVo watchVo = WatchMap.getVo(watchCode);
        watchVo.setIsOpen(false);
        return R.success();
    }

    @PostMapping("/un/watch/stop")
    public R stopWatch(@RequestBody WatchVo watchVo) {
        watchUtil.endWatch(watchVo);
        return R.success();
    }

    @PostMapping("/un/watch/test")
    public R doWrite(@RequestBody Map<String, String> map) {
        String filePath = map.get("filePath");
        String times = map.get("times");
        if (StrUtil.isBlank(filePath)) {
            return R.error("请输入文件地址");
        }
        File f = new File(filePath);
        if (!f.exists()) {
            return R.error("文件不存在");
        }
        int max = 5;
        if (StrUtil.isNotBlank(times)) {
            try {
                max = Integer.valueOf(times);
            } catch (Exception e) {
                max = 5;
            }
        }
        for (int i = 0; i < max; i++) {
            try {
                Thread.sleep(RandomUtil.randomInt(10) * 500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // 使用 try-with-resources 语句自动关闭资源
            // true 表示以追加模式打开文件
            try (FileWriter fw = new FileWriter(filePath, true);
                 BufferedWriter bw = new BufferedWriter(fw)) {
                int maxLine = RandomUtil.randomInt(1, 20);
                for (int j = 0; j < maxLine; j++) {
                    bw.newLine();
                    String newData = RandomUtil.randomString(RandomUtil.randomInt(50, 100));
                    bw.write(newData);
                }
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.success();
    }
}
